using System;
using UnityEngine;
#if CHARTBOOST_ADS
using ChartboostSDK;
#endif
using System.Collections.Generic;
using System.Collections;
using UnityEngine.Events;

#pragma warning disable 0168 // variable declared but not used.
#pragma warning disable 0414 // variable assigned but not used.

namespace IndieStudio.BugsBang.Utility
{

	[DisallowMultipleComponent]
	public class ChartboostAd : MonoBehaviour
	{
#if CHARTBOOST_ADS
		private List<string> delegateHistory;
		private bool ageGate = false;
		private bool autocache = true;
		private bool activeAgeGate = false;
		private bool showInterstitial = true;
		private bool showMoreApps = true;
		private bool showRewardedVideo = true;
		private UnityEvent onShowAdsEvent;

#if UNITY_IPHONE
				private CBStatusBarBehavior statusBar = CBStatusBarBehavior.Ignore;
#endif

		void Awake()
		{
			// Create the Chartboost gameobject with the editor AppId and AppSignature
			Chartboost.Create();
		}

		void OnEnable() {
		SetupDelegates();
		}

		void Start(){
		delegateHistory = new List<string>();
#if UNITY_IPHONE
				Chartboost.setShouldPauseClickForConfirmation(ageGate);
#endif
		Chartboost.setAutoCacheAds(autocache);
		Chartboost.setMediation (CBMediation.AdMob, "1.0");
		Chartboost.addDataUseConsent(CBGDPRDataUseConsent.Behavioral);

		Debug.Log("Is Initialized: " + Chartboost.isInitialized());
		}

		void SetupDelegates()
		{
			// Listen to all impression-related events
			Chartboost.didInitialize += didInitialize;
			Chartboost.didFailToLoadInterstitial += didFailToLoadInterstitial;
			Chartboost.didDismissInterstitial += didDismissInterstitial;
			Chartboost.didCloseInterstitial += didCloseInterstitial;
			Chartboost.didClickInterstitial += didClickInterstitial;
			Chartboost.didCacheInterstitial += didCacheInterstitial;
			Chartboost.shouldDisplayInterstitial += shouldDisplayInterstitial;
			Chartboost.didDisplayInterstitial += didDisplayInterstitial;
			Chartboost.didFailToRecordClick += didFailToRecordClick;
			Chartboost.didFailToLoadRewardedVideo += didFailToLoadRewardedVideo;
			Chartboost.didDismissRewardedVideo += didDismissRewardedVideo;
			Chartboost.didCloseRewardedVideo += didCloseRewardedVideo;
			Chartboost.didClickRewardedVideo += didClickRewardedVideo;
			Chartboost.didCacheRewardedVideo += didCacheRewardedVideo;
			Chartboost.shouldDisplayRewardedVideo += shouldDisplayRewardedVideo;
			Chartboost.didCompleteRewardedVideo += didCompleteRewardedVideo;
			Chartboost.didDisplayRewardedVideo += didDisplayRewardedVideo;
			Chartboost.didPauseClickForConfirmation += didPauseClickForConfirmation;
			Chartboost.willDisplayVideo += willDisplayVideo;
#if UNITY_IPHONE
				Chartboost.didCompleteAppStoreSheetFlow += didCompleteAppStoreSheetFlow;
#endif
		}

		void AddLog(string text)
		{
		Debug.Log(text);
		delegateHistory.Insert(0, text + "\n");
		int count = delegateHistory.Count;
		if( count > 20 )
		{
		delegateHistory.RemoveRange(20, count-20);
		}
		}

		public bool HasInterstitial(){
		return  Chartboost.hasInterstitial(CBLocation.Default);
		}

		public bool HasRewardedVideo(){
		return Chartboost.hasRewardedVideo(CBLocation.Default);;
		}

		public void CacheInterstitial(){
		Chartboost.cacheInterstitial(CBLocation.Default);
		}

		public void ShowInterstitial(UnityEvent onShowAdsEvent){
			this.onShowAdsEvent = onShowAdsEvent;
			Chartboost.showInterstitial(CBLocation.Default);
		}

		public void CacheRewardedVideo(){
		Chartboost.cacheRewardedVideo(CBLocation.Default);
		}

		public void  ShowRewardedVideo(UnityEvent onShowAdsEvent){
			this.onShowAdsEvent = onShowAdsEvent;
			Chartboost.showRewardedVideo(CBLocation.Default);
		}

		public void SendPIAMainLevelEvent(){
		Chartboost.trackLevelInfo("Test Data", CBLevelType.HIGHEST_LEVEL_REACHED, 1, "Test Send mail level Information");
		}

		public void SendPIASubLevelEvent(){
		Chartboost.trackLevelInfo("Test Data", CBLevelType.HIGHEST_LEVEL_REACHED, 1, 2, "Test Send sub level Information");
		}

		void OnDisable() {
			// Remove event handlers
			Chartboost.didInitialize -= didInitialize;
			Chartboost.didFailToLoadInterstitial -= didFailToLoadInterstitial;
			Chartboost.didDismissInterstitial -= didDismissInterstitial;
			Chartboost.didCloseInterstitial -= didCloseInterstitial;
			Chartboost.didClickInterstitial -= didClickInterstitial;
			Chartboost.didCacheInterstitial -= didCacheInterstitial;
			Chartboost.shouldDisplayInterstitial -= shouldDisplayInterstitial;
			Chartboost.didDisplayInterstitial -= didDisplayInterstitial;
			Chartboost.didFailToRecordClick -= didFailToRecordClick;
			Chartboost.didFailToLoadRewardedVideo -= didFailToLoadRewardedVideo;
			Chartboost.didDismissRewardedVideo -= didDismissRewardedVideo;
			Chartboost.didCloseRewardedVideo -= didCloseRewardedVideo;
			Chartboost.didClickRewardedVideo -= didClickRewardedVideo;
			Chartboost.didCacheRewardedVideo -= didCacheRewardedVideo;
			Chartboost.shouldDisplayRewardedVideo -= shouldDisplayRewardedVideo;
			Chartboost.didCompleteRewardedVideo -= didCompleteRewardedVideo;
			Chartboost.didDisplayRewardedVideo -= didDisplayRewardedVideo;
			Chartboost.didPauseClickForConfirmation -= didPauseClickForConfirmation;
			Chartboost.willDisplayVideo -= willDisplayVideo;
#if UNITY_IPHONE
					Chartboost.didCompleteAppStoreSheetFlow -= didCompleteAppStoreSheetFlow;
#endif
		}


		void didInitialize(bool status)
		{
			AddLog(string.Format("didInitialize: {0}", status));
		}

		void didFailToLoadInterstitial(CBLocation location, CBImpressionError error)
		{
			AddLog(string.Format("didFailToLoadInterstitial: {0} at location {1}", error, location));
		}

		void didDismissInterstitial(CBLocation location)
		{
			AddLog("didDismissInterstitial: " + location);
		}

		void didCloseInterstitial(CBLocation location)
		{
			if (onShowAdsEvent != null)
				onShowAdsEvent.Invoke();

			AddLog("didCloseInterstitial: " + location);
		}

		void didClickInterstitial(CBLocation location)
		{
			AddLog("didClickInterstitial: " + location);
		}

		void didCacheInterstitial(CBLocation location)
		{
			AddLog("didCacheInterstitial: " + location);
		}

		bool shouldDisplayInterstitial(CBLocation location)
		{
			// return true if you want to allow the interstitial to be displayed
			AddLog("shouldDisplayInterstitial @" + location + " : " + showInterstitial);
			return showInterstitial;
		}

		void didDisplayInterstitial(CBLocation location)
		{
			AddLog("didDisplayInterstitial: " + location);
		}

		void didFailToRecordClick(CBLocation location, CBClickError error)
		{
			AddLog(string.Format("didFailToRecordClick: {0} at location: {1}", error, location));
		}

		void didFailToLoadRewardedVideo(CBLocation location, CBImpressionError error)
		{
			AddLog(string.Format("didFailToLoadRewardedVideo: {0} at location {1}", error, location));
		}

		void didDismissRewardedVideo(CBLocation location)
		{
			AddLog("didDismissRewardedVideo: " + location);
		}

		void didCloseRewardedVideo(CBLocation location)
		{
			if (onShowAdsEvent != null)
				onShowAdsEvent.Invoke();
			AddLog("didCloseRewardedVideo: " + location);
		}

		void didClickRewardedVideo(CBLocation location)
		{
			AddLog("didClickRewardedVideo: " + location);
		}

		void didCacheRewardedVideo(CBLocation location)
		{
			AddLog("didCacheRewardedVideo: " + location);
		}

		bool shouldDisplayRewardedVideo(CBLocation location)
		{
			AddLog("shouldDisplayRewardedVideo @" + location + " : " + showRewardedVideo);
			return showRewardedVideo;
		}

		void didCompleteRewardedVideo(CBLocation location, int reward)
		{
			AddLog(string.Format("didCompleteRewardedVideo: reward {0} at location {1}", reward, location));
		}

		void didDisplayRewardedVideo(CBLocation location)
		{
			AddLog("didDisplayRewardedVideo: " + location);
		}

		void didPauseClickForConfirmation()
		{
#if UNITY_IPHONE
				AddLog("didPauseClickForConfirmation called");
				activeAgeGate = true;
#endif
		}

		void willDisplayVideo(CBLocation location)
		{
			AddLog("willDisplayVideo: " + location);
		}

#if UNITY_IPHONE
		void didCompleteAppStoreSheetFlow() {
			AddLog("didCompleteAppStoreSheetFlow");
		}

		void TrackIAP() {
			// The iOS receipt data from Unibill is already base64 encoded.
			// Others store kit plugins may be different.
			// This is a sample sandbox receipt.
			string sampleReceipt = @"ewoJInNpZ25hdHVyZSIgPSAiQXBNVUJDODZBbHpOaWtWNVl0clpBTWlKUWJLOEVk
	ZVhrNjNrV0JBWHpsQzhkWEd1anE0N1puSVlLb0ZFMW9OL0ZTOGNYbEZmcDlZWHQ5
	aU1CZEwyNTBsUlJtaU5HYnloa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HRnpaUzFrWVhSbExXMXpJaUE5SUNJ
	eE16TTFOems0TXpVMU9EWTRJanNLQ1NKd2NtOWtkV04wTFdsa0lpQTlJQ0pqYjIw
	dWJXbHVaRzF2WW1Gd2NDNWtiM2R1Ykc5aFpDSTdDZ2tpYVhSbGJTMXBaQ0lnUFNB
	aU5USXhNVEk1T0RFeUlqc0tDU0ppYVdRaUlEMGdJbU52YlM1dGFXNWtiVzlpWVhC
	d0xrMXBibVJOYjJJaU93b0pJbkIxY21Ob1lYTmxMV1JoZEdVdGJYTWlJRDBnSWpF
	ek16VTNPVGd6TlRVNE5qZ2lPd29KSW5CMWNtTm9ZWE5sTFdSaGRHVWlJRDBnSWpJ
	d01USXRNRFF0TXpBZ01UVTZNRFU2TlRVZ1JYUmpMMGROVkNJN0Nna2ljSFZ5WTJo
	aGMyVXRaR0YwWlMxd2MzUWlJRDBnSWpJd01USXRNRFF0TXpBZ01EZzZNRFU2TlRV
	Z1FXMWxjbWxqWVM5TWIzTmZRVzVuWld4bGN5STdDZ2tpYjNKcFoybHVZV3d0Y0hW
	eVkyaGhjMlV0WkdGMFpTSWdQU0FpTWpBeE1pMHdOQzB6TUNBeE5Ub3dOVG8xTlNC
	RmRHTXZSMDFVSWpzS2ZRPT0iOwoJImVudmlyb25tZW50IiA9ICJTYW5kYm94IjsK
	CSJwb2QiID0gIjEwMCI7Cgkic2lnbmluZy1zdGF0dXMiID0gIjAiOwp9";

			// Demonstrate Base64 encoding. Not necessary for the data above
			// If the receipt was not base64 encoded, send encodedText not sampleReceipt 
			//byte[] bytesToEncode = Encoding.UTF8.GetBytes(sampleReceipt);
			//string encodedText = Convert.ToBase64String(bytesToEncode);

			// Send the receipt for track an In App Purchase Event
			Chartboost.trackInAppAppleStorePurchaseEvent(sampleReceipt,
				"sample product title", "sample product description", "1.99", "USD", "sample product identifier" );
			//byte[] decodedText = Convert.FromBase64String(sampleReceipt);
			//Debug.Log("Decoded: " + System.Text.Encoding.UTF8.GetString(decodedText));
			//Debug.Log("Encoded: " + encodedText);
		}
#elif UNITY_ANDROID
		void TrackIAP()
		{
			Debug.Log("TrackIAP");
			// title, description, price, currency, productID, purchaseData, purchaseSignature
			// This data should be sent after handling the results from the google store.
			// This is fake data and doesn't represent a real or even imaginary purchase
			Chartboost.trackInAppGooglePlayPurchaseEvent("SampleItem", "TestPurchase", "0.99", "USD", "ProductID", "PurchaseData", "PurchaseSignature");

			// If you are using the Amazon store...		
			//Chartboost.trackInAppAmazonStorePurchaseEvent("SampleItem", "TestPurchase", "0.99", "ProductID", "UserId", "PurchaseToken");
		}
#else
		void TrackIAP() {
			Debug.Log("TrackIAP on unsupported platform");
		}
#endif


#endif
	}
}